package com.rz.core.kit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisKit {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public Long getHashCacheKeySize(String cacheKey) {
        return redisTemplate.opsForHash().size(cacheKey);
    }


    public void putHash(String cacheKey, String key, Object value) {
        redisTemplate.opsForHash().put(cacheKey, key, value);
    }

    public void setValue(String key, Object value, long timeout) {
        if (timeout == 0) {
            redisTemplate.opsForValue().set(key, value);
        } else {
            redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MINUTES);
        }
    }

    public List<Object> multiGet(String cacheKey,List<Object> keys){
        return redisTemplate.opsForHash().multiGet(cacheKey,keys);
    }

    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public List<Object> getValuesByCacheKey(String cacheKey) {
        return redisTemplate.opsForHash().values(cacheKey);
    }

    public Set<Object> getKeysByCacheKey(String cacheKey) {
        return redisTemplate.opsForHash().keys(cacheKey);
    }

    public List<Object> getValuesByCacheKey(String cacheKey, Integer page, Integer pageSize) {
        List<Object> result = redisTemplate.opsForHash().values(cacheKey);
        int size = result.size();
        int startIndex = 0;
        int endIndex = size;
        if (page != null && pageSize != null) {
            startIndex = page * pageSize - pageSize < size ? page * pageSize - pageSize : size;
            endIndex = page * pageSize < size ? page * pageSize : size;
        }
        return result.subList(startIndex, endIndex);
    }

    public Object getValueHash(String cacheKey, Object key) {
        return redisTemplate.opsForHash().get(cacheKey, key.toString());
    }

    public void deleteHash(String cacheKey) {
        redisTemplate.delete(cacheKey);
    }

    public void deleteHash(String cacheKey, Object... keys) {
        redisTemplate.opsForHash().delete(cacheKey, keys);
    }

    public boolean containsKeyHash(String cacheKey, Object key) {
        return redisTemplate.opsForHash().hasKey(cacheKey, key.toString());
    }

    public void saveSocketConnectStatus(String key, Object val) {
        redisTemplate.opsForList().leftPush(key, val);
        redisTemplate.expire(key, 12, TimeUnit.HOURS);
    }

    public Long leftPush(String key, Object val) {
        return redisTemplate.opsForList().leftPush(key, val);
    }

    public Long rightPush(String key, Object val) {
        return redisTemplate.opsForList().rightPush(key, val);
    }

    public Long rightPushAll(String key, Collection<Object> list) {
        return redisTemplate.opsForList().rightPushAll(key, list);
    }

    public Object rightPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }

    public Object leftPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }
}